package com.cskaoyan.config;

import com.cskaoyan.shiro.CustomRealm;
import com.cskaoyan.shiro.WxCustomRealm;
//import com.sun.deploy.services.Service;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
//import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * shiro组件的使用
 */
@Configuration
public class ShiroConfig {
    /**
     * 总结：提供SecurityManager的组件，放入容器
     */


    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm realm,
                                                     WxCustomRealm wxCustomRealm,
                                                     DefaultWebSessionManager sessionManager,
                                                     CustomAuthenticator authenticator) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        List<Realm> realms = new ArrayList<>();
        realms.add(realm);
        realms.add(wxCustomRealm);
        securityManager.setAuthenticator(authenticator);
        securityManager.setRealms(realms);
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }


    /**
     * 总结：在DispatcherServlet中生成代理对象，为filter提供一系列的url和filter之间的映射关系（直接）
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/admin/auth/login", "anon");    //对于登录login，一般都使用：anon匿名
        filterChainDefinitionMap.put("/admin/auth/info","anon");
        filterChainDefinitionMap.put("/admin/auth/401","anon");

//        filterChainDefinitionMap.put("admin/auth/logout","logout");
        filterChainDefinitionMap.put("/wx/auth/login","anon");  // 登录页
        filterChainDefinitionMap.put("/wx/home/index","anon");  // 首页
        filterChainDefinitionMap.put("/wx/catalog/index","anon");  // 分类页
        filterChainDefinitionMap.put("/wx/goods/category","anon");  // 商品类目
        filterChainDefinitionMap.put("/wx/goods/list","anon");  // 商品列表
        filterChainDefinitionMap.put("/wx/brand/detail","anon");  // 品牌详情
        filterChainDefinitionMap.put("/wx/goods/detail","anon");  // 商品详情
        filterChainDefinitionMap.put("/wx/search/index","anon");  // 搜索主页
        filterChainDefinitionMap.put("/**", "authc");    //所有的请求都需要认证
        shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilter;
    }

    //advisor
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }



    @Bean
    public CustomAuthenticator authenticator(CustomRealm marketRealm,WxCustomRealm wxCustomRealm){
        CustomAuthenticator customAuthenticator = new CustomAuthenticator();
        // 告诉customAuthenticator，我们添加了两个realms
        ArrayList<Realm> realms = new ArrayList<>();
        realms.add(marketRealm);
        realms.add(wxCustomRealm);
        customAuthenticator.setRealms(realms);
        return customAuthenticator;
    }
}
